clear
clear matrix
clear mata
*set maxvar 2000
set more off
use "C:\Users\Andy Baker\OneDrive - UCB-O365\Stata\Barry\Final\OmnibusBoth.dta", clear
*drop if suspect1==1 | suspect2==1 | suspect3==1
drop if suspect==1

*GEN SES
capture program drop getaggs
program define getaggs

while "`1'"~="" {
egen `1'sum=sum(`1') if respond==1, by(bairroamob wave)
gen `1'smnon=`1'sum-`1'
gen n`1'=0
replace n`1'=1 if `1'~=.
egen sumn`1'=sum(n`1') if respond==1, by(bairroamob wave)
gen `1'bairronomr=`1'smnon/(sumn`1'-1)
gen `1'bairroall=`1'sum/sumn`1' 
bysort bairroamob wave: egen `1'bairroallmean=max(`1'bairroall)
replace `1'bairronomr=`1'bairroallmean if `1'bairronomr==.
drop `1'sum n`1' sumn`1' `1'smnon `1'bairroall `1'bairroallmean
mac shift
}
end

mvdecode s6_education, mv(18)
mvdecode s6_education, mv(19)
mvdecode s12_incomefamily, mv(1)
mvdecode s12_incomefamily, mv(8)
mvdecode s12_incomefamily, mv(9)
replace s12_incomefamily=10000 if s12_incomefamily>10000 & s12_incomefamily~=.
recode s12_incomefamily 0=.
gen educ=ln(s6_education)
gen lnincome=ln(s12_incomefamily+1)
getaggs lnincome

*CONVERSATION FREQUENCY
recode v16b_conversenotamob v16e_conversefriends v16g_conversefamily (1=3) (2=2) (3=1) (4=0) (8=.) (9=.) (10=.)

polychoricpca v16b_conversenotamob v16e_conversefriends v16g_conversefamily, score(conversetemp) nscore(1) 
impute conversetemp v16b_conversenotamob v16e_conversefriends v16g_conversefamily if respond==1, gen(converse)

*GENERATE MEDIA ATTENTION SCORE (IV)
replace v7b1fq_mediatv1choicefreq=0 if v7a_mediatvyesorno==2
replace v7b2fq_mediatv2choicefreq=0 if v7a_mediatvyesorno==2
replace v8b1fq_mediamags1choicefreq=0 if v8a_mediamagsyesorno==2
replace v8b2fq_mediamags2choicefreq=0 if v8a_mediamagsyesorno==2
replace v9b1fq_mediapaper1choicefreq=0 if v9a_mediapaperyesorno==2
replace v9b2fq_mediapaper2choicefreq=0 if v9a_mediapaperyesorno==2
replace v10b_mediaradiofreq=10 if v10b_mediaradiofreq>10 & v10b_mediaradiofreq<61
replace v10b_mediaradiofreq=0 if v10a_mediaradioyesorno==2

mvdecode v7b1fq_mediatv1choicefreq v7b2fq_mediatv2choicefreq v8b1fq_mediamags1choicefreq v8b2fq_mediamags2choicefreq v9b1fq_mediapaper1choicefreq v9b2fq_mediapaper2choicefreq v10b_mediaradiofreq, mv(8)
mvdecode v7b1fq_mediatv1choicefreq v7b2fq_mediatv2choicefreq v8b1fq_mediamags1choicefreq v8b2fq_mediamags2choicefreq v9b1fq_mediapaper1choicefreq v9b2fq_mediapaper2choicefreq v10b_mediaradiofreq, mv(9)

factor v7b1fq_mediatv1choicefreq v7b2fq_mediatv2choicefreq v8b1fq_mediamags1choicefreq v8b2fq_mediamags2choicefreq v9b1fq_mediapaper1choicefreq v9b2fq_mediapaper2choicefreq v10b_mediaradiofreq if respond==1, pcf factors(1) 
predict mediaattentiontemp 
impute mediaattentiontemp  v7b1fq_mediatv1choicefreq v7b2fq_mediatv2choicefreq v8b1fq_mediamags1choicefreq v8b2fq_mediamags2choicefreq v9b1fq_mediapaper1choicefreq v9b2fq_mediapaper2choicefreq v10b_mediaradiofreq if respond==1, gen(mediaattention)

xtset id wave
*VOLATILITY
	*MR change
gen change23=0 if respond==1 & l.respond==1 & l.v22c_presvote~=3 & v22c_presvote~=3 & l.v22c_presvote~=6 & v22c_presvote~=9 & l.v22c_presvote<17 & v22c_presvote<17 & wave==3
replace change23=1 if l.v22c_presvote~=v22c_presvote & change23==0 & wave==3

	*D1 change
gen d_change231=0 if respond==1 & l.respond==1 & l.v74c_disc1presvote~=3 & v74c_disc1presvote~=3 & l.v74c_disc1presvote~=6 & v74c_disc1presvote~=9 & l.v74c_disc1presvote<17 & v74c_disc1presvote<17 & wave==3
replace d_change231=1 if l.v74c_disc1presvote~=v74c_disc1presvote & d_change231==0 & wave==3

	*D2 change
gen d_change232=0 if respond==1 & l.respond==1 & l.v75c_disc2presvote~=3 & v75c_disc2presvote~=3 & l.v75c_disc2presvote~=6 & v75c_disc2presvote~=9 & l.v75c_disc2presvote<17 & v75c_disc2presvote<17 & wave==3
replace d_change232=1 if l.v75c_disc2presvote~=v75c_disc2presvote & d_change232==0 & wave==3

	*D3 change
gen d_change233=0 if respond==1 & l.respond==1 & l.v76c_disc3presvote~=3 & v76c_disc3presvote~=3 & l.v76c_disc3presvote~=6 & v76c_disc3presvote~=9 & l.v76c_disc3presvote<17 & v76c_disc3presvote<17 & wave==3
replace d_change233=1 if l.v76c_disc3presvote~=v76c_disc3presvote & d_change233==0 & wave==3

	*D4 change
gen d_change234=0 if respond==1 & l.respond==1 & l.v77c_disc4presvote~=3 & v77c_disc4presvote~=3 & l.v77c_disc4presvote~=6 & v77c_disc4presvote~=9 & l.v77c_disc4presvote<17 & v77c_disc4presvote<17 & wave==3
replace d_change234=1 if l.v77c_disc4presvote~=v77c_disc4presvote & d_change234==0 & wave==3

*GEN AWARENESS FOR COMPARISON TO DISCUSSANTS
	*MR
gen correct2d=0  if respond==1
replace correct2d=1 if s14b_know2vicepres==2
gen correct3d=0  if respond==1
replace correct3d=1 if s14c_know3fhcparty==3
gen correct4d=0  if respond==1
replace correct4d=1 if s14d_know4mercosul==2

*Awareness index is based on results of this IRT model 
/*
	irt 2pl correct2d correct3d correct4d if respond==1
	predict awaretemp, latent ebmean
	egen awaretempz=std(awaretemp)
	reg awaretempz correct2d correct3d correct4d
*/

gen aware_comparetodisc=correct2d*1.32083+correct3d*.90794832 +correct4d* .882677-1.755655 
replace aware_comparetodisc=. if wave==2 | wave>3
gen l2aware_comparetodisc=l2.aware_comparetodisc
	*note rather minimal learning of these answers between waves 1 and waves 3
ttest l2aware_comparetodisc==aware_comparetodisc if wave==3

ci mean aware_comparetodisc if wave==1 
mat mean1=r(mean)
mat ub1=r(ub)
mat lb1=r(lb)

ci mean aware_comparetodisc if wave==3
mat mean2=r(mean)
mat ub2=r(ub)
mat lb2=r(lb)

recode v43a_pidyesorno (2=0) (1=1) (else=.), gen(pid)
gen l2pid=l2.pid

	*D1
gen d_respond1=0
replace d_respond1=1 if d1_date~=.
gen d1_correct2d=0 if d_respond1==1
replace d1_correct2d=1 if d1s14b_know2vicepres==2
gen d1_correct3d=0  if d_respond1==1
replace d1_correct3d=1 if d1s14c_know3fhcparty==3
gen d1_correct4d=0  if d_respond1==1
replace d1_correct4d=1 if d1s14d_know4mercosul==2
gen d_aware_comparetodisc1=d1_correct2d*1.32083+d1_correct3d*.90794832 +d1_correct4d*.882677-1.755655
recode d1v43a_pidyesorno (2=0) (1=1) (else=.), gen(d_pid1)
replace d_pid1=. if d_respond1~=1
gen d_vote1=d1v22c_presvote
replace d_vote1=. if d_respond1~=1
gen d_orderinmainrespondentlist1= d1_orderinmainrespondentlist

	*D2
gen d_respond2=0
replace d_respond2=1 if d2_date~=.
gen d2_correct2d=0 if d_respond2==1
replace d2_correct2d=1 if d2s14b_know2vicepres==2
gen d2_correct3d=0  if d_respond2==1
replace d2_correct3d=1 if d2s14c_know3fhcparty==3
gen d2_correct4d=0  if d_respond2==1
replace d2_correct4d=1 if d2s14d_know4mercosul==2
gen d_aware_comparetodisc2=d2_correct2d*1.32083+d2_correct3d*.90794832 +d2_correct4d*.882677-1.755655
recode d2v43a_pidyesorno (2=0) (1=1) (else=.), gen(d_pid2)
gen d_vote2=d2v22c_presvote
replace d_vote2=. if d_respond2~=1
gen d_orderinmainrespondentlist2=d2_orderinmainrespondentlist

gen lv74b_disc1relate=l.v74b_disc1relate
gen lv75b_disc2relate=l.v75b_disc2relate
gen lv76b_disc3relate=l.v76b_disc3relate
gen lv77b_disc4relate=l.v77b_disc4relate

gen lv74c_disc1presvote=l.v74c_disc1presvote  
gen lv75c_disc2presvote=l.v75c_disc2presvote
gen lv76c_disc3presvote=l.v76c_disc3presvote
gen lv77c_disc4presvote=l.v77c_disc4presvote

*percent of don't knows
tab v74c_disc1presvote if wave==3
tab v75c_disc2presvote if wave==3
tab v76c_disc3presvote if wave==3



preserve

keep id wave respond d_respond1 d_respond2 d_aware_comparetodisc1 d_aware_comparetodisc2 aware_comparetodisc l2aware_comparetodisc ///
pid d_pid1 d_pid2 d_vote1 d_vote2 v74c_disc1presvote v75c_disc2presvote v76c_disc3presvote v77c_disc4presvote d_orderinmainrespondentlist1 ///
d_orderinmainrespondentlist2 d_vote1 d_vote2 v22c_presvote city lv74b_disc1relate lv75b_disc2relate lv76b_disc3relate lv77b_disc4relate ///
lv74c_disc1presvote lv75c_disc2presvote  lv76c_disc3presvote lv77c_disc4presvote change23 d_change231 d_change232 d_change233 d_change234 l2pid ///
v39a_persuadetoothersyesorno 
reshape long d_respond d_aware_comparetodisc d_pid d_vote d_orderinmainrespondentlist , i(id wave) j(discid)

/*NOTICE THIS!*/ drop if wave>3 

	*Accuracy of Perceptions
gen perceived_disc_vote=v74c_disc1presvote if d_orderinmainrespondentlist==1
replace perceived_disc_vote=v75c_disc2presvote if d_orderinmainrespondentlist==2
replace perceived_disc_vote=v76c_disc3presvote if d_orderinmainrespondentlist==3
replace perceived_disc_vote=v77c_disc4presvote if d_orderinmainrespondentlist==4
tab d_vote perceived_disc_vote if wave==3
gen correct=0 if d_vote<17 & perceived_disc_vote<17 & wave==3
replace correct=1 if perceived_disc_vote==d_vote & correct==0
tab correct
gen project=0 if correct~=.
replace project=1 if correct==0 & perceived_disc_vote==2 & v22c_presvote==2
replace project=1 if correct==0 & perceived_disc_vote==1 & v22c_presvote==1
replace project=1 if correct==0 & perceived_disc_vote==4 & v22c_presvote==4
replace project=1 if correct==0 & perceived_disc_vote==5 & v22c_presvote==5
tab project correct, col
gen correct_abstain=0 if d_vote<18 & perceived_disc_vote<18 & wave==3
replace correct_abstain=1 if perceived_disc_vote==d_vote & correct_abstain==0
tab correct_abstain

gen disc_relate=lv74b_disc1relate if d_orderinmainrespondentlist==1
replace disc_relate=lv75b_disc2relate if d_orderinmainrespondentlist==2
replace disc_relate=lv76b_disc3relate if d_orderinmainrespondentlist==3
replace disc_relate=lv77b_disc4relate if d_orderinmainrespondentlist==4

	*Awareness 
ci mean d_aware_comparetodisc if wave==3 
mat mean3=r(mean)
mat ub3=r(ub)
mat lb3=r(lb)

ttest d_aware_comparetodisc== l2aware_comparetodisc
mat mean4=r(mu_1)-r(mu_2) 
mat ub4=mean4+r(se)*1.96
mat lb4=mean4-r(se)*1.96

ttest d_aware_comparetodisc== aware_comparetodisc
mat mean5=r(mu_1)-r(mu_2) 
mat ub5=mean5+r(se)*1.96
mat lb5=mean5-r(se)*1.96

ttest d_aware_comparetodisc== l2aware_comparetodisc if d_orderinmainrespondentlist==1
mat mean6=r(mu_1)-r(mu_2)
mat ub6=mean6+r(se)*1.96
mat lb6=mean6-r(se)*1.96

ttest d_aware_comparetodisc== l2aware_comparetodisc if d_orderinmainrespondentlist==2
mat mean7=r(mu_1)-r(mu_2)
mat ub7=mean7+r(se)*1.96
mat lb7=mean7-r(se)*1.96

ttest d_aware_comparetodisc== l2aware_comparetodisc if d_orderinmainrespondentlist==3
mat mean8=r(mu_1)-r(mu_2)
mat ub8=mean8+r(se)*1.96
mat lb8=mean8-r(se)*1.96

ci mean d_aware_comparetodisc if d_orderinmainrespondentlist==1
ci mean d_aware_comparetodisc if d_orderinmainrespondentlist==3

mat mean=.
mat ub=.
mat lb=.

mat mean=[mean8\mean7\mean6\mean5\mean4\mean3\mean2\mean1]
mat lb=[lb8\lb7\lb6\lb5\lb4\lb3\lb2\lb1]
mat ub=[ub8\ub7\ub6\ub5\ub4\ub3\ub2\ub1]

svmat mean
svmat lb 
svmat ub

gen n=_n
replace n=. if n>8

gen meanmarker=round(mean,.001)
replace meanmarker=. if n==8
gen str meanmarker0=".000" if n==8
gen str number="{it:N}=4,741" if n==8
replace number="{it:N}=5,066" if n==7 
replace number="{it:N}=2,810" if n==6
replace number="{it:N}=2,778" if n==5
replace number="{it:N}=2,430" if n==4
replace number="{it:N}=1,358" if n==3
replace number="{it:N}=821" if n==2
replace number="{it:N}=496" if n==1

gen number_place=.45
*Figure 2.6
twoway (rcap ub lb n, color(black) horizontal ) (scatter n mean1, mcolor(black) mlab(meanmarker) mlabpos(12) mlabcolor(black)) ///
(scatter n mean1, mcolor(black) mlab(meanmarker0) mlabpos(5) mlabcolor(black)) ///
(scatter n number_place, msym(i) mlab(number) mlabpos(0) mlabcolor(black)) , ///
xtitle("{bf:Political Knowledge Score}" "(in standard deviations)") ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ///
xline(0, lcolor(black)) ylab(8 "Egos{sub:{it:pc}}" 7 "Egos{sub:{it:e}}" 6 "Alters{sub:{it:e}}" ///
5 "Alter{sub:{it:e}} - Ego{sub:{it:pc}}" 4 "Alter{sub:{it:e}} - Ego{sub:{it:e}}"  3 "Alter1{sub:{it:e}} - Ego{sub:{it:pc}}" 2 "Alter2{sub:{it:e}} - Ego{sub:{it:pc}}" 1 "Alter3{sub:{it:e}} - Ego{sub:{it:pc}}", angle(horizontal)) ///
ylab(,nogrid) xlab(0 .1 .2 .3 .4 .5, grid) legend(off) yline(3.5 5.5, lcolor(black)) ytitle("")

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\TwoCityKnowledge.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_6.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_6.pdf", as(pdf) replace 

	*PID

gen 	disc_change=d_change231 if d_orderinmainrespondentlist==1
replace disc_change=d_change232 if d_orderinmainrespondentlist==2
replace disc_change=d_change233 if d_orderinmainrespondentlist==3
replace disc_change=d_change234 if d_orderinmainrespondentlist==4
tab change23 if discid==1
tab disc_change if correct==1

bysort d_orderinmainrespondentlist: ci prop d_pid
bysort d_orderinmainrespondentlist: ci prop disc_change if correct==1

restore

keep id wave lnincome lnincomebairronomr educ city converse mediaattention v38a_persuadefrompartyyesorno v38b2b_persuade2frompartyname v38b3b_persuade3frompartyname v74_disc1named v75_disc2named v76_disc3named v74b_disc1relate v75b_disc2relate v76b_disc3relate  responsepattern responsepattern3wave respond whennew  s1_sex s11a_raceintrvwr s11b_raceself s14acs_know1cargo s14ajf_know1cargo s14b_know2vicepres s14c_know3fhcparty s14d_know4mercosul s14ecs_know5senator s14ejf_know5senator s14f_know6preschamber s14bW4_know2vicepres s14fW5_know6preschamber s14ics_know9simonparty s14ijf_know9aecioparty aware_comparetodisc s14gcs_know7mayor s14gjf_know7mayor s14hcs_know8cargopauletti
reshape xij lnincome lnincomebairronomr educ city converse mediaattention v38a_persuadefrompartyyesorno v38b2b_persuade2frompartyname v38b3b_persuade3frompartyname v74_disc1named v75_disc2named v76_disc3named v74b_disc1relate v75b_disc2relate v76b_disc3relate respond whennew  s1_sex  s11a_raceintrvwr s11b_raceself s14acs_know1cargo s14ajf_know1cargo s14b_know2vicepres s14c_know3fhcparty s14d_know4mercosul s14ecs_know5senator s14ejf_know5senator s14f_know6preschamber s14bW4_know2vicepres  s14fW5_know6preschamber s14ics_know9simonparty s14ijf_know9aecioparty aware_comparetodisc s14gcs_know7mayor s14gjf_know7mayor s14hcs_know8cargopauletti
reshape wide

impute lnincome1 lnincome4 lnincome5 lnincomebairronomr1 if respond1==1, gen(wealth1)
impute lnincome2 lnincome4 lnincome5  lnincomebairronomr2 if whennew2==2, gen(wealth2)
impute lnincome4 lnincome1 lnincome5 lnincomebairronomr4 if respond4==1, gen(wealth4)

*party contact
recode v38a_persuadefrompartyyesorno3 (2=0) (8/9=.)
gen partycontact3=0 if respond3==1
replace partycontact3=1 if v38a_persuadefrompartyyesorno3 ==1
replace partycontact3=2 if v38b2b_persuade2frompartyname3~=.
replace partycontact3=3 if v38b3b_persuade3frompartyname3~=.

recode v38a_persuadefrompartyyesorno6 (2=0) (8/9=.)
gen partycontact6=0 if respond6==1
replace partycontact6=1 if v38a_persuadefrompartyyesorno6==1
replace partycontact6=2 if v38b2b_persuade2frompartyname6~=.
replace partycontact6=3 if v38b3b_persuade3frompartyname6~=.

*COUNT DISCUSSANTS (IV)
egen disctotal2=rsum(v74_disc1named2 v75_disc2named2 v76_disc3named2)
replace disctotal2=. if respond2==0
replace disctotal2=. if whennew2==2

egen disctotal5=rsum(v74_disc1named5 v75_disc2named5 v76_disc3named5)
replace disctotal5=. if respond5==0

recode v74b_disc1relate2 v75b_disc2relate2 v76b_disc3relate2 (1/3=1) (4/6=0) (7/18=-1) (.=-1), gen(relate12 relate22 relate32)
replace relate12=. if v74_disc1named2~=1
replace relate22=. if v75_disc2named2~=1
replace relate32=. if v76_disc3named2~=1

egen disctotalfam2=anycount(relate12 relate22 relate32), values(1)
replace disctotalfam2=. if respond2~=1
replace disctotalfam2=. if whennew2==2

egen disctotalfrnd2=anycount(relate12 relate22 relate32), values(0)
replace disctotalfrnd2=. if respond2~=1
replace disctotalfrnd2=. if whennew2==2

egen disctotalunspec2=anycount(relate12 relate22 relate32), values(-1)
replace disctotalunspec2=. if respond2~=1
replace disctotalunspec2=. if whennew2==2
	
recode v74b_disc1relate5 v75b_disc2relate5 v76b_disc3relate5 (1/3=1) (4/6=0) (7/18=-1) (.=-1), gen(relate15 relate25 relate35)
replace relate15=. if v74_disc1named5~=1
replace relate25=. if v75_disc2named5~=1
replace relate35=. if v76_disc3named5~=1

egen disctotalfam5=anycount(relate15 relate25 relate35), values(1)
replace disctotalfam5=. if respond5~=1

egen disctotalfrnd5=anycount(relate15 relate25 relate35), values(0)
replace disctotalfrnd5=. if respond5~=1

egen disctotalunspec5=anycount(relate15 relate25 relate35), values(-1)
replace disctotalunspec5=. if respond5~=1

tab disctotal2
tab disctotal5

ci mean disctotal2
ci mean disctotalfrnd2
ci mean disctotalfam2
ci mean disctotalunspec2

ci mean disctotal5
ci mean disctotalfrnd5
ci mean disctotalfam5
ci mean disctotalunspec5


*GEN THE AWARENESS VARIABLE.  Note its a little tricky because people answered a different number depending on which wave/s they were in
*Awareness 2002
gen correct1cs=0 if respond1==1 & city1==1
replace correct1cs=2 if s14acs_know1cargo1==1 & correct1cs==0 
		* Give half a point to people who said Ana Corso was a Fed. Dep. because she was a suplente for 8 months
replace correct1cs=1 if s14acs_know1cargo1==2 & correct1cs==0 
gen correct1jf=0 if respond1==1 & city1==2
replace correct1jf=1 if s14ajf_know1cargo1==2 & correct1jf==0 
gen correct2=0 if respond1==1
replace correct2=1 if s14b_know2vicepres1==2 & respond1==1
gen correct3=0 if respond1==1
replace correct3=1 if s14c_know3fhcparty1==3 & respond1==1
gen correct4=0 if respond1==1
replace correct4=1 if s14d_know4mercosul1==2 & respond1==1
gen correct5cs=0 if respond1==1 & city1==1
replace correct5cs=1 if s14ecs_know5senator1==1 & correct5cs==0
gen correct5jf=0 if respond1==1 & city1==2
replace correct5jf=1 if s14ejf_know5senator1==1 & correct5jf==0
gen correct6=0 if respond1==1
replace correct6=1 if s14f_know6preschamber1==3 & respond1==1

*this is for the fresh respondents
replace correct1cs=0 if respond2==1 & whennew1==2 & city1==1
replace correct1cs=2 if s14acs_know1cargo2==1 & respond2==1 & whennew1==2 & city1==1
replace correct1cs=1 if s14acs_know1cargo2==2 & respond2==1 & whennew1==2 & city1==1
replace correct1jf=0 if respond2==1 & whennew1==2 & city1==2
replace correct1jf=1 if s14ajf_know1cargo2==2 & respond2==1 & whennew1==2 & city1==2
replace correct2=0 if respond2==1 & whennew1==2 
replace correct2=1 if s14b_know2vicepres2==2 & respond2==1 & whennew1==2
replace correct3=0 if respond2==1 & whennew1==2 
replace correct3=1 if s14c_know3fhcparty2==3 & respond2==1 & whennew1==2
replace correct4=0 if respond2==1 & whennew1==2 
replace correct4=1 if s14d_know4mercosul2==2 & respond2==1 & whennew1==2
replace correct5cs=0 if respond2==1 & whennew1==2 & city1==1
replace correct5cs=1 if s14ecs_know5senator2==1 & respond2==1 & whennew1==2 & city1==1
replace correct5jf=0 if respond2==1 & whennew1==2 & city1==2
replace correct5jf=1 if s14ejf_know5senator2==1 & respond2==1 & whennew1==2 & city1==2
replace correct6=0 if respond2==1 & whennew1==2 
replace correct6=1 if s14f_know6preschamber2==3 & respond2==1 & whennew1==2

replace correct2=0 if respond3==1 & whennew1==3 
replace correct2=1 if s14b_know2vicepres3==2 & respond3==1 & whennew1==3
replace correct3=0 if respond3==1 & whennew1==3 
replace correct3=1 if s14c_know3fhcparty3==3 & respond3==1 & whennew1==3
replace correct4=0 if respond3==1 & whennew1==3 
replace correct4=1 if s14d_know4mercosul3==2 & respond3==1 & whennew1==3

gen correct7cs=0 if respond3==1 & city1==1
replace correct7cs=1 if s14gcs_know7mayor3==4
gen correct7jf=0 if respond3==1 & city1==2
replace correct7jf=1 if s14gjf_know7mayor3==4

irt grm correct1cs correct1jf correct2 correct3 correct4 correct5cs correct5jf correct6 correct7cs correct7jf
predict awaretemp , latent ebmeans
egen aware1z=std(awaretemp) 

*Awareness 2006
drop correct*
gen correct11cs=0 if respond5==1 & city1==1
replace correct11cs=1 if s14hcs_know8cargopauletti5==3 & correct11cs==0 
gen correct12=0 if respond5==1
replace correct12=1 if s14bW4_know2vicepres5==4 & respond5==1
gen correct14=0 if respond5==1
replace correct14=1 if s14d_know4mercosul5==2 & respond5==1
gen correct16=0 if respond5==1
replace correct16=1 if s14fW5_know6preschamber5==4 & respond5==1
gen correct15cs=0 if respond5==1 & city1==1
replace correct15cs=1 if s14ecs_know5senator5==1 & correct15cs==0
gen correct15jf=0 if respond5==1 & city1==2
replace correct15jf=1 if s14ejf_know5senator5==1 & correct15jf==0
gen correct17cs=0 if respond5==1 & city1==1
replace correct17cs=1 if s14gcs_know7mayor5==3
gen correct17jf=0 if respond5==1 & city1==2
replace correct17jf=1 if s14gjf_know7mayor5==1
gen correct13cs=0 if respond5==1 & city1==1
replace correct13cs=1 if s14ics_know9simonparty5==2

irt 2pl correct11cs correct12 correct14 correct15cs correct15jf correct16 correct17cs correct17jf correct13cs if respond5==1
predict awaretemp5 if respond5==1, latent ebmeans
egen aware2z=std(awaretemp5) if respond5==1

*Start regs

*
*2002: Switch between these four lines and the four below to determine which election
egen disctotalmean=rowmean(disctotal2)
egen mediaattentionmean=rowmean(mediaattention2 mediaattention3)
egen conversemean=rowmean(converse2 converse3)
gen partycontact=partycontact3
*/
*
/*2006: 
egen disctotalmean=rowmean(disctotal5)
egen mediaattentionmean=rowmean(mediaattention5 mediaattention6)
egen conversemean=rowmean(converse5 converse6)
gen partycontact=partycontact6
*/
egen mediaattention_std=std(mediaattentionmean)
egen disctotalmean_std=std(disctotalmean)
egen conversemean_std=std(conversemean)
egen partycontact_std=std(partycontact)

*sex
recode s1_sex1 s1_sex2 s1_sex3 s1_sex4 s1_sex5 s1_sex6 (2=0), gen(male1 male2 male3 male4 male5 male6)
gen male=male1 if whennew1==1
replace male=male2 if whennew1==2
replace male=male3 if whennew1==3

*race
recode s11a_raceintrvwr1 s11a_raceintrvwr2 s11a_raceintrvwr3 s11a_raceintrvwr4 s11a_raceintrvwr5 s11a_raceintrvwr6 (1=3) (2=2) (3=1) (4=2) (5=1) (8/9=.), gen(race_intrvwr1 race_intrvwr2 race_intrvwr3 race_intrvwr4 race_intrvwr5 race_intrvwr6)
recode s11b_raceself1 s11b_raceself2 s11b_raceself3 s11b_raceself4 s11b_raceself5 s11b_raceself6 (1=3) (2=2) (3=1) (4=2) (5=1) (8/9=.), gen(race_self1 race_self2 race_self3 race_self4 race_self5 race_self6)
egen racemean=rowmean(race_intrvwr1 race_self1 race_intrvwr2 race_self2 race_intrvwr3 race_self3)


egen wealth=rowmean(wealth1 wealth2 wealth4)
egen educ=rowmean(educ1 educ2 educ3 educ4 educ5 educ6)

label variable wealth "Wealth"
label variable educ "Education level"
label variable male "Male"
label variable racemean "Race"
label variable mediaattention_std "Frequency of political news consumption"
label variable conversemean_std "Frequency of political discussion"
label variable disctotalmean_std "Egocentric network size"
label variable partycontact_std "Contacted by party"

*Figure 8.1A and B, Table A.21
eststo clear
eststo: reg conversemean_std wealth educ male racemean city1
margin, at((p5) wealth educ) at((p95) wealth educ) contrast(atcontrast(r)) atmeans
mat bwealth1=r(b)
mat vwealth1=r(V)
margin, at((p5) male) at((p95) male) contrast(atcontrast(r)) atmeans
mat bsex1=r(b)
mat vsex1=r(V)
margin, at((p5) racemean) at((p95) racemean) contrast(atcontrast(r)) atmeans
mat brace1=r(b)
mat vrace1=r(V)

eststo: reg disctotalmean_std wealth educ male racemean city1
margin, at((p5) wealth educ) at((p95) wealth educ) contrast(atcontrast(r)) atmeans
mat bwealth2=r(b)
mat vwealth2=r(V)
margin, at((p5) male) at((p95) male) contrast(atcontrast(r)) atmeans
mat bsex2=r(b)
mat vsex2=r(V)
margin, at((p5) racemean) at((p95) racemean) contrast(atcontrast(r)) atmeans
mat brace2=r(b)
mat vrace2=r(V)

eststo: reg mediaattention_std wealth educ male racemean city1
margin, at((p5) wealth educ) at((p95) wealth educ) contrast(atcontrast(r)) atmeans
mat bwealth3=r(b)
mat vwealth3=r(V)
margin, at((p5) male) at((p95) male) contrast(atcontrast(r)) atmeans
mat bsex3=r(b)
mat vsex3=r(V)
margin, at((p5) racemean) at((p95) racemean) contrast(atcontrast(r)) atmeans
mat brace3=r(b)
mat vrace3=r(V)

eststo: reg partycontact_std wealth educ male racemean city1
margin, at((p5) wealth educ) at((p95) wealth educ) contrast(atcontrast(r)) atmeans
mat bwealth4=r(b)
mat vwealth4=r(V)
margin, at((p5) male) at((p95) male) contrast(atcontrast(r)) atmeans
mat bsex4=r(b)
mat vsex4=r(V)
margin, at((p5) racemean) at((p95) racemean) contrast(atcontrast(r)) atmeans
mat brace4=r(b)
mat vrace4=r(V)

mat bwealth5=.
mat vwealth5=.
mat bsex5=.
mat vsex5=.
mat brace5=.
mat vrace5=.

mat Bwealth=[bwealth5\bwealth4\bwealth3\bwealth2\bwealth1]
mat Bsex=[bsex5\bsex4\bsex3\bsex2\bsex1]
mat Brace=[brace5\brace4\brace3\brace2\brace1]

mat Vwealth=[vwealth5\vwealth4\vwealth3\vwealth2\vwealth1]
mat Vsex=[vsex5\vsex4\vsex3\vsex2\vsex1]
mat Vrace=[vrace5\vrace4\vrace3\vrace2\vrace1]

svmat Bwealth 
svmat Bsex 
svmat Brace 
svmat Vwealth 
svmat Vsex 
svmat Vrace
gen nwealth=_n+.125
gen nsex=_n
gen nrace=_n-.125
gen n=_n
gen wealthupper=Bwealth+1.9645*(Vwealth^.5)
gen wealthlower=Bwealth-1.9645*(Vwealth^.5)
gen sexupper=Bsex+1.9645*(Vsex^.5)
gen sexlower=Bsex-1.9645*(Vsex^.5)
gen raceupper=Brace+1.9645*(Vrace^.5)
gen racelower=Brace-1.9645*(Vrace^.5)

*Figure 8.1
*with legend
twoway (rcap wealthupper wealthlower nwealth if n<6, color(black) horizontal ) (scatter nwealth Bwealth if n<6, mcolor(black)) ///
(rcap sexupper sexlower nsex if n<6, color(black) horizontal ) (scatter nsex Bsex if n<6, mcolor(black) msymbol(S)) ///
(rcap raceupper racelower nrace if n<6, color(black) horizontal ) (scatter nrace Brace if n<6, mcolor(black) msymbol(t) msize(medlarge)) , ///
xtitle("Effect (in standard deviations) on DV" "of change from min to max in SES, gender, or race") ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("DV: Intermediary Form") ///
legend(order(2 "SES" 4 "Gender (male)" 6 "Race (white)") colgap(20) col(1) color(black)) ///
xline(0, lcolor(black)) ylab(5 "Freq. of pol. discussion" 4 "Egocentric network size"  3 "Freq. of pol. news" 2 "Contact by party" 1 "Payoff by party", angle(horizontal)) ///
ylab(,nogrid) xlab(-.5 -.25 0 .25 .5 .75 1 1.25 1.5 1.75 2.0, grid) 
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Figures\legend.tif", as(tif) replace
*graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_1legend.tif", as(tif) replace width(15000)

twoway (rcap wealthupper wealthlower nwealth if n<6, color(black) horizontal ) (scatter nwealth Bwealth if n<6, mcolor(black)) ///
(rcap sexupper sexlower nsex if n<6, color(black) horizontal ) (scatter nsex Bsex if n<6, mcolor(black) msymbol(S) msize(medsmall)) ///
(rcap raceupper racelower nrace if n<6, color(black) horizontal ) (scatter nrace Brace if n<6, mcolor(black) msymbol(t) msize(medlarge)) , ///
xtitle("{bf:Model-Predicted Change in DV}" "(upon changing SES, gender, or race" "from min to max)", size(medium)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("{bf:Dependent Variables (DV):}" "{bf:Exposure to Political Intermediaries}", size(medium)) ///
legend(off) ///
xline(0, lcolor(black)) ylab(5 "Freq. of pol. discussion" 4 "Egocentric network size"  3 "Media exposure" 2 "Contacted by a party" 1 "Paid off by a party", angle(horizontal) labsize(medium)) ///
ylab(,nogrid) xlab(-.5 0 .5 1 1.5 2.0, grid) xscale(range(-.5 2.3)) yline(1.5 2.5 3.5 4.5, lcolor(black))
	*remember you have to toggle the stuff above to get the correct year
*graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Figures\Brazil 2002.tif", as(tif) replace
*graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_1A.tif", as(tif) replace width(1500)
*graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_1A.pdf", as(pdf) replace 
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Figures\Brazil 2006.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_1B.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_1B.pdf", as(pdf) replace 

*for appendix
cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Output\"
esttab est1 est2 est3 est4 using Brazil2006strat.rtf, label b(3) replace se star(* 0.10 ** 0.05 ) nogap onecell title(Table: Correlates of Intermediation Exposure, Brazil 2006) 

*AWARENESS 3 FOR AWARENESS AND SES/GENDER FIGURE. USE FIRST WAVE B/C IT HAS BEST SAMPLING PROPERTIES. 
*Figure 8.3, Table A.23
reg aware1z wealth1 educ male city1
margin, at((p5) wealth1 educ) at((p95) wealth1 educ) contrast(atcontrast(r)) atmeans
mat bwealthBRA02=r(b)
mat vwealthBRA02=r(V)
margin, at((p5) male) at((p95) male) contrast(atcontrast(r))  atmeans
mat bsexBRA02=r(b)
mat vsexBRA02=r(V)

reg aware2z wealth1 educ male city5
margin, at((p5) wealth1 educ) at((p95) wealth1 educ) contrast(atcontrast(r)) atmeans
mat bwealthBRA06=r(b)
mat vwealthBRA06=r(V)
margin, at((p5) male) at((p95) male) contrast(atcontrast(r))  atmeans
mat bsexBRA06=r(b)
mat vsexBRA06=r(V)

*esttab est1 est2 using Brazilaware0206strat.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Awareness by sex and gender Brazil 2002/06)

xyz
